# 创建一个新的plugin工程
# 输入一个plugin名，创建plugin名文件夹
# 在该文件夹下创建Source文件夹，存放plugin的源代码
# 在Source下创建plugin名.h和.cpp文件
# 创建CMakeLists.txt文件，用于编译plugin

import os
import sys
import shutil
import argparse

def create_plugin(plugin_name):
    # 创建plugin名文件夹
    plugin_dir = os.path.join(os.getcwd(), "plugins", plugin_name)
    os.makedirs(plugin_dir, exist_ok=True)

    # 创建Source文件夹
    source_dir = os.path.join(plugin_dir, "src")
    os.makedirs(source_dir, exist_ok=True)

    # 创建plugin名.h和.cpp文件
    header_file = os.path.join(source_dir, f"{plugin_name}.h")
    cpp_file = os.path.join(source_dir, f"{plugin_name}.cpp")
    with open(header_file, "w") as f:
              content = f"""\
#pragma once

#include \"Plugin/PluginBase.h\"

PLUGIN_DEFINE({plugin_name})
{{
public:
  PLUGIN_NAME({plugin_name})

  void Init(){{}};
}};

PLUGIN_EXPORT({plugin_name})

EXPORT_FUNC(Init)
"""
              f.write(content)
              
    with open(cpp_file, "w") as f:
              content = f"""\
#include \"{plugin_name}.h\"
"""
              f.write(content)

    # 创建CMakeLists.txt文件
    cmake_file = os.path.join(plugin_dir, "CMakeLists.txt")
    with open(cmake_file, "w") as f:
        content = f"""\
cmake_minimum_required(VERSION 3.22.1)
project({plugin_name})
file(GLOB_RECURSE SRCS "./src/**")
add_library(${{PROJECT_NAME}} SHARED ${{SRCS}})
target_include_directories(${{PROJECT_NAME}} PUBLIC
    src
    ../../src
)

set(APP_DIR ../../../build/src/App/${{CMAKE_BUILD_TYPE}})
set(PLUGINS_DIR ${{APP_DIR}}/plugins)
message(${{APP_DIR}}/plugins)

add_custom_command(TARGET ${{PROJECT_NAME}} POST_BUILD
    COMMAND ${{CMAKE_COMMAND}} -E copy $<TARGET_FILE:${{PROJECT_NAME}}> ${{PLUGINS_DIR}}/$<TARGET_FILE_NAME:${{PROJECT_NAME}}>
)
"""
        f.write(content)
        
    print(f"Plugin '{plugin_name}' created successfully.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Create a new plugin.")
    parser.add_argument("plugin_name", help="The name of the plugin to create.")
    args = parser.parse_args()
    create_plugin(args.plugin_name)
    